package com.java.gui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class Calculator implements ActionListener{
	private JFrame jf;
	private JButton clearButton;
	private JTextField field;
	private JButton[] allButtons;
	public  Calculator(){
		this.jf=new JFrame("我的计算器v1.1");
		clearButton=new JButton("Clear");
		this.field=new JTextField(20);
		
		String str="123+456-789*0.=/";
		this.allButtons=new JButton[16];
		for(int i=0;i<str.length();i++){
			allButtons[i]=new JButton(str.substring(i,i+1));
		}
	}
	//负责布局管理
	public void inition(){
		this.jf.setLayout(new BorderLayout());
		JPanel northPanel=new JPanel();
		JPanel centerPanel=new JPanel();
		this.jf.add(northPanel,BorderLayout.NORTH);
		this.jf.add(centerPanel, BorderLayout.CENTER);
		northPanel.setLayout(new FlowLayout());
		northPanel.add(this.field);
		northPanel.add(this.clearButton);
		centerPanel.setLayout(new GridLayout(4,4));
		for(int i=0;i<allButtons.length;i++){
			centerPanel.add(this.allButtons[i]);
		}

	}
	//负责字体颜色和大小
	public void setFontAndColor(){
		Font f=new Font("宋体",Font.BOLD,20);
		this.field.setFont(f);
		this.field.setEditable(false);
		field.setHorizontalAlignment(JTextField.RIGHT);
		for(int i=0;i<this.allButtons.length;i++){
			allButtons[i].setFont(f);
//			allButtons[i].setBackground(Color.ORANGE);
			allButtons[i].setForeground(Color.BLUE);
		}
	}
	//设置显示效果
	public void showMe(){
		inition();
		setFontAndColor();
		addEventHandler();
//		jf.setSize(300, 200);
		
		jf.pack();//自动调整合适大小
		jf.setVisible(true);
		jf.setLocation(350, 250);//出现的位置
		jf.setResizable(false);//是否可以调整大小
		
		jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}
	//添加事件监听器
	public void addEventHandler(){
		for(int i=0;i<this.allButtons.length;i++){
			allButtons[i].addActionListener(this);
		}
		clearButton.addActionListener(new ActionListener(){

			public void actionPerformed(ActionEvent e) {
				field.setText("");
			}
			
		});
	}

	public void actionPerformed(ActionEvent e) {
		String com=e.getActionCommand().trim();
		int count=0;
		char ch='+';
		double result1=0.0,result2=0.0;
		if("0123456789.".indexOf(com)!=-1){
			field.setText(field.getText()+com);
			if(count==0){
				result1=Double.valueOf(field.getText());
				System.out.println(result1);
			}else{
				result2=Double.valueOf(field.getText());
				System.out.println(result2);

			}
		    
		    
		}else if("+-*/".indexOf(com)!=-1){
			ch=com.charAt(0);
			this.field.setText("");
			count++;
//			ch=com.charAt(0);
		}else if("=".indexOf(com)!=-1){
			this.field.setText("");
		    switch(ch){
		    case '+':result1+=result2;break;
		    case '-':result1-=result2;break;
		    case '*':result1*=result2;break;
		    case '/':result1/=result2;;
		    }
		    field.setText(Double.toString(result1));

		}
	}
	
	public static void main(String[] args){
		new Calculator().showMe();
	}
  }
